<template>
    <div class="container">
        <h3>包装指令</h3>
        <el-row style="margin:15px 0 20px 0">
            <el-col :span="7">
                <el-input placeholder="请输入关键词" style="width:300px" v-model.trim="searchName">
                    <i slot="prefix" class="el-input__icon el-icon-search"></i>
                    <el-button slot="append" @click="handleSearch">搜索</el-button>
                </el-input>
            </el-col>
            <router-link :to="switchHeadTab == '生产系统'?'/packing/addPacking':'/packing/addPackingMedicalHerb'"><el-button type="primary" v-if="added" style="float:right; margin-right: 10px">新增包装</el-button></router-link>
        </el-row>
        <el-row>
            <el-table
                :data="tableData"
                border
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
				<el-table-column v-if='switchHeadTab == "生产系统"' prop="inventoryType" sortable label="产品大类"></el-table-column>
                <el-table-column prop="breedName" sortable label="名称"></el-table-column>
                <el-table-column prop="batch" sortable label="产品批号"></el-table-column>
                <el-table-column prop="time" sortable label="生产日期"></el-table-column>
				 <el-table-column prop="deadLine" sortable label="保质期（月）"></el-table-column>
                <el-table-column prop="scUseNum" sortable label="投料量">
                    <template slot-scope="scope">
                        {{scope.row.scUseNum}}{{scope.row.scUseNumUnits}}
                    </template>
                </el-table-column>
             <!--   <el-table-column prop="totalNumber" sortable label="实际产量">
                    <template slot-scope="scope">
                        <span v-if="scope.row.totalNumber == ''"></span>
                        <span v-else>
                            {{scope.row.totalNumber}}{{scope.row.units}}
                        </span>
                    </template>
                </el-table-column> -->
                <el-table-column label="操作" width="150" align="center">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                @click="handleView(scope.$index, scope.row)"
                            >查看</el-button>
                            <el-button
                                type="text"
                                v-if="edit"
                                v-show="scope.row.isRecall == 0"
                                @click="handleEdit(scope.$index, scope.row)"
                                style="color: #666"
                            >编辑</el-button>
                            <!-- <el-button type="text" v-if="scope.row.isRecall == 0"> -->
                                <!-- <el-dropdown style="color: #409EFF; font-size:12px"> -->
                                    <!-- <span class="el-dropdown-link">
                                        更多
                                    </span> -->
                                    <!-- <el-dropdown-menu slot="dropdown"> -->
                                        <!-- <el-dropdown-item> -->
                                            <!-- <el-button
                                                type="text"
                                                v-if="edit"
                                                @click="handleEdit(scope.$index, scope.row)"
                                                style="color: #666"
                                            >编辑</el-button> -->
                                        <!-- </el-dropdown-item> -->
                                        <!-- <el-dropdown-item>
                                            <el-button
                                                type="text"
                                                v-show="dels"
                                                @click="handleFrmLoss(scope.$index, scope.row)"
                                                style="color: #666"
                                            >实际产量</el-button>
                                        </el-dropdown-item> -->
                                    <!-- </el-dropdown-menu> -->
                                <!-- </el-dropdown> -->
                            <!-- </el-button> -->
                            <el-button
                                v-show="scope.row.isRecall == 1"
                                type="info"
                                disabled
                            >已召回</el-button>
                        </template>
                    </el-table-column>
            </el-table>
            <el-pagination layout="total, prev, pager, next, jumper" :page-size="pagesize1" @current-change="handleCurrentChange1" :total="total1" :current-page.sync="currentPage1" style="margin-top:20px"></el-pagination>
        </el-row>
        <el-dialog title="实际产量" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                <el-row>
                    <el-col :span="16">
                        <el-form-item label="调整日期：" label-width="120px" prop="time">
                            <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm.time" style="width: 300px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="实际产量：" prop="newNumber" label-width="120px">
                            <el-input v-model.trim="ruleForm.newNumber" style="width:300px">
                                <template slot="append">

                                    <!-- {{ruleForm.units}} -->
                                    <el-select style="width:80px;" v-model="ruleForm.units" :disabled="disabled" placeholder="单位">
                                        <el-option v-for="item in unitslist" :key="item.id" :label="item.name" :value="item.name">{{item.name}}</el-option>
                                    </el-select>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="操作人：" prop="relevantUser" label-width="120px">
                            <el-select v-model="ruleForm.relevantUser" filterable placeholder="请选择" value-key="id" @change="changeRelevant" style="width:300px">
                                <el-option
                                    v-for="item in userList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item"
                                >
                                {{item.name}}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {pack_findByList,user_findAll,manufacture_findById,pack_adjust,pack_findById,unit_findByList} from "@/request/api";
import { mapState } from 'vuex';
    export default {
        data() {
            return {
                dialogFormVisible: false,
                searchName: '',
                tableData: [
                    {
                        id: '11',
                        breedName: '1211',
                        batch: '',
                        time: ''
                    }
                ],
                disabled:false,
                total1: 0,
                unitslist:'',
                currentPage1:1,
                pagesize1:10,
                ruleForm: {
                    time: '',
                    relevantUser: '',
                    relevantUserId: '',
                    newNumber: '',
                    units: '',
                },
                rules:{
                    time: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    relevantUser: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    newNumber: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ]
                },
                userList: [],
                added:true,
                edit:true,
                dels:true
            };
        },
        created(){
            var data = JSON.stringify(this.$store.state.kl)
            var a = "新增"
            var b = "编辑"
            var c = "实际产量"
            if(data.indexOf(a) != -1){
                this.added = true
            }else{
                this.added = false
            }
            if(data.indexOf(b) != -1){
                this.edit = true
            }else{
                this.edit = false
            }
            if(data.indexOf(c) != -1){
                this.dels = true
            }else{
                this.dels = false
            }
        },
        mounted(){
            this.init()
            this.units()
            // console.log(this.$store.state.newStorge)
        },
        computed:{
            ...mapState(['switchHeadTab'])
        },
        methods: {
            // 获取单位
            units(){
                unit_findByList("").then((res)=>{
                    this.unitslist = res.data
                })
            },
            // 调用接口
            init(){
                pack_findByList({
                    materialType:this.switchHeadTab == '中药饮片' ? this.switchHeadTab:'',
                    searchName: this.searchName,
                    page: this.currentPage1,
                }).then((res) =>{
                    this.tableData = res.rows
                    this.total1 = res.total
                })
            },
            // 编辑
            handleEdit(index,row){
                if(this.switchHeadTab == '生产系统'){
                    this.$router.push({
                        path: '/packing/addPacking',
                        query: { id: row.id }
                    });
                }else{
                    this.$router.push({
                        path: '/packing/addPackingMedicalHerb',
                        query: { id: row.id }
                    });
                }

            },
            // 查看
            handleView(index,row){
                this.$router.push({
                    path: '/packing/viewPacking',
                    query: { id: row.id }
                });
            },
            // 选择实际产量相关人
            changeRelevant(ele){
                this.ruleForm.relevantUserId = ele.id
                this.ruleForm.relevantUser = ele.name
            },
            // 实际产量
            handleFrmLoss(index,row){
                this.ruleForm.time = ""
                this.ruleForm.newNumber = ""
                this.ruleForm.relevantUser = ""
                this.dialogFormVisible = true
                user_findAll('').then((res)=>{
                    this.userList = res.data

                })
                pack_findById(row.id).then((res) =>{
                    this.ruleForm.id = res.data.id
                    this.ruleForm.userNumber = res.data.userNumber
                    this.ruleForm.units = res.data.units
                    this.ruleForm.type = res.data.type
                    console.log(res.data)
                    if(this.ruleForm.units != null && this.ruleForm.units != "" && this.ruleForm.units != "null"){
                        this.disabled = true
                    }else{
                        this.disabled = false
                    }
                })
            },
            // 提交产量
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // console.log(typeof this.ruleForm.userNumber)
                        // if (parseInt(this.ruleForm.newNumber) > parseInt(this.ruleForm.userNumber)) {
                            // console.log()
                            var parm = {
                                type: this.ruleForm.type,
                                relevanceId: this.ruleForm.id,
                                time: this.ruleForm.time,
                                relevantUser: this.ruleForm.relevantUser,
                                relevantUserId: this.ruleForm.relevantUserId,
                                newNumber: this.ruleForm.newNumber,
                                units: this.ruleForm.units,
                            }
                            console.log(parm)
                            pack_adjust(parm).then((res) => {
                                this.$notify({
                                    title: '提示',
                                    message: '调整成功',
                                    type: 'success',
                                    duration:1000
                                });
                                this.dialogFormVisible = false
                                this.init();
                                // if (this.materialType == '提取饮片') {
                                //     this.init1()
                                // } else if(this.materialType == '配方颗粒') {
                                //     this.init2()
                                // }
                                // else if(this.materialType == '颗粒包装'){
                                //     this.init3()
                                // }
                            })
                        // } else{
                        //     this.$notify.error({
                        //         title: '提示',
                        //         message: '实际产量不能小于剩余量'+this.ruleForm.userNumber+'',
                        //         showClose: false,
                        //         duration:3000
                        //     });
                        // }
                    } else {
                        return false;
                    }
                })
            },
            // 搜索
            handleSearch() {
                this.init();
            },
            handleCurrentChange1(val){
                this.currentPage1 = val
                this.init()
            },
        }
    }
</script>

<style scoped>
.el-pagination{
    text-align: center;
}
.el-col{
    margin-bottom: 0;
}
.el-select .el-input {
width: 130px;
}
.input-with-select .el-input-group__prepend {
background-color: #fff;
}
.el-input--small .el-input__inner{
    border-radius: 5px 0 0 5px !important;
    border-right: none;
}
</style>
